package com.contory.ssn_lgnd.app.maingame;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

import com.contory.ssn_lgnd.app.comp.DrawPtr;
import com.contory.ssn_lgnd.app.comp.UpdatePtr;
import com.contory.ssn_lgnd.app.comp.ViewObject;
import com.contory.ssn_lgnd.framework.gl.Camera2D;
import com.contory.ssn_lgnd.framework.gl.SpriteBatcher;
import com.contory.ssn_lgnd.framework.gl.TextureRegion;
import com.contory.ssn_lgnd.framework.math.OverlapTester;
import com.contory.ssn_lgnd.framework.math.Rectangle;

public class BackGroundLayer implements DrawPtr, UpdatePtr{
	LinkedList<BackGroundObject> objList;
	float objW = 4;
	float objH = 8;
	float wPadding = objW / 8;
	final Camera2D cam;
	
	public BackGroundLayer(float x, float y, float w, float h, TextureRegion tr, Camera2D cam){
		this.cam = cam;
		int viewObjNum = (int)(w / (objW + 2 * wPadding));
		float strX = x - w/2;
		objList = new LinkedList<BackGroundObject>();
		objList.add(new BackGroundObject(strX + wPadding, y, objW, objH, tr));
		for(int i = 1; i < viewObjNum; i ++){
			ViewObject front = objList.get(i-1).bodyView;
			objList.add(new BackGroundObject(front.x + objW + wPadding * 2, front.y, objW, objH, tr));
		}
	}
	
	@Override
	public void update(float deltaTime) {
		/*
		BackGroundObject chd = objList.getFirst();
		if(!OverlapTester.overlapRectangles(cam.getBoundRect(), chd.boundRect)){
			objList.remove(chd);
			chd.setPos(objList.getLast().bodyView.x + wPadding * 2, objList.getLast().bodyView.y);
			objList.add(chd);
		}
		
		chd = objList.getLast();
		if(!OverlapTester.overlapRectangles(cam.getBoundRect(), chd.boundRect)){
			objList.remove(chd);
			chd.setPos(objList.getFirst().bodyView.x, objList.getFirst().bodyView.y);
			objList.addFirst(chd);
		}
		*/
	}

	@Override
	public void draw(SpriteBatcher batcher) {
		for(int i = 0; i < objList.size(); i++){
			BackGroundObject chd = objList.get(i);
			chd.draw(batcher);
		}
	}
	
	class BackGroundObject implements DrawPtr{
		ViewObject bodyView;
		boolean enableBool = true;
		Rectangle boundRect;
		public BackGroundObject(float x, float y, float w, float h, TextureRegion tr){
			bodyView = new ViewObject();
			bodyView.setXY_WH_TR(x, y, w, h, tr);
			boundRect = new Rectangle(x - w/2, y - h/2, w, h);
		}
		
		public void setPos(float x, float y){
			bodyView.x = x;
			bodyView.y = y;
			rectUpdate(x, y);
		}
		
		private void rectUpdate(float x, float y){
			boundRect.lowerLeft.set(x - bodyView.w / 2, y - bodyView.h / 2);
		}
		
		@Override
		public void draw(SpriteBatcher batcher) {
			batcher.drawSprite(bodyView.x, bodyView.y, bodyView.w, bodyView.h, 0, 0.4314f, 0.4784f, 0.5f, bodyView.tr);
		}
	}
}
